//package com.psl.servlet;
//
//import com.psl.services.frame.fileUploadServices;
//import org.apache.commons.fileupload.FileItem;
//import org.apache.commons.fileupload.disk.DiskFileItemFactory;
//import org.apache.commons.fileupload.servlet.ServletFileUpload;
//
//import javax.servlet.ServletException;
//import javax.servlet.http.HttpServlet;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import javax.servlet.http.HttpSession;
//import java.io.*;
//import java.util.Iterator;
//import java.util.List;
//
//public class UploadServlet
//  extends HttpServlet
//{
//  protected void doGet(HttpServletRequest request, HttpServletResponse response)
//    throws ServletException, IOException
//  {
//    doPost(request, response);
//  }
//
//  protected void doPost(HttpServletRequest request, HttpServletResponse response)
//    throws ServletException, IOException
//  {
//    String filetype = request.getParameter("filetype");
//    String order_id = request.getParameter("order_id");
//    request.setCharacterEncoding("utf-8");
//    response.setCharacterEncoding("utf-8");
//    response.setContentType("text/html");
//    HttpSession session = request.getSession();
//    session.setAttribute("prog", "0");
//    session.setAttribute("result", "NOK");
//    session.setAttribute("error", "");
//    String error = "";
//    int maxSize = 1073741824;
//    DiskFileItemFactory factory = new DiskFileItemFactory();
//    ServletFileUpload upload = new ServletFileUpload(factory);
//    upload.setHeaderEncoding("utf-8");
//    upload.setFileSizeMax(1073741824L);
//    try
//    {
//      List items = upload.parseRequest(request);
//      Iterator itr = items.iterator();
//      while (itr.hasNext())
//      {
//        FileItem item = (FileItem)itr.next();
//        if (!item.isFormField()) {
//          if ((item.getName() != null) && (!item.getName().equals("")))
//          {
//            long upFileSize = item.getSize();
//            String fileName = item.getName();
//            String str = fileName.substring(fileName.lastIndexOf(".") + 1);
////            String uuidname = CommonUtils.uuid() + "." + str;
//            String size = String.valueOf(item.getSize());
//            if (upFileSize > maxSize)
//            {
//              error = "文件太大";
//              break;
//            }
//            File file = new File("C:\\webfile\\" + filetype, uuidname);
////            File file = new File("/Users/mioko/Desktop/" + filetype, uuidname);
//
//            InputStream is = item.getInputStream();
//            int length = 0;
//            byte[] by = new byte['?'];
//            double persent = 0.0D;
//            FileOutputStream fos = new FileOutputStream(file);
//            PrintWriter out = response.getWriter();
//            fileUploadServices fps = new fileUploadServices();
//            fps.insertfile(fileName, uuidname, filetype, size, order_id);
//            while ((length = is.read(by)) != -1)
//            {
//              persent += length / upFileSize * 100.0D;
//              fos.write(by, 0, length);
//              session.setAttribute("prog", Math.round(persent) + "");
//              Thread.sleep(10L);
//            }
//            fos.close();
//            Thread.sleep(1000L);
//          }
//          else
//          {
//            error = "没选择文件";
//          }
//        }
//      }
//    }
//    catch (Exception e)
//    {
//      e.printStackTrace();
//      error = "文件上传出现错误:" + e.getMessage();
//    }
//    if (!error.equals("")) {
//      session.setAttribute("error", error);
//    } else {
//      session.setAttribute("result", "OK");
//    }
//  }
//}
